#!/bin/bash

echo "天气预报APP打包并运行脚本"
echo "==========================="
echo

# 检查Node.js是否安装
if ! command -v node &> /dev/null; then
    echo "错误: 未检测到Node.js，请先安装Node.js"
    echo "下载地址: https://nodejs.org/"
    exit 1
fi

# 检查Expo CLI是否安装
if ! npx expo --version &> /dev/null; then
    echo "安装Expo CLI..."
    npm install -g expo-cli
    if [ $? -ne 0 ]; then
        echo "错误: 安装Expo CLI失败"
        exit 1
    fi
fi

# 检查EAS CLI是否安装
if ! npx eas --version &> /dev/null; then
    echo "安装EAS CLI..."
    npm install -g eas-cli
    if [ $? -ne 0 ]; then
        echo "错误: 安装EAS CLI失败"
        exit 1
    fi
fi

echo "安装项目依赖..."
cd 天气预报APP
npm install
if [ $? -ne 0 ]; then
    echo "错误: 安装依赖失败"
    exit 1
fi

echo
echo "请选择操作:"
echo "1. 运行应用（开发模式）"
echo "2. 打包Android应用"
echo "3. 打包iOS应用"
echo "4. 打包Web应用"
echo "5. 退出"
echo

read -p "请输入选项 (1-5): " choice

case $choice in
    1)
        echo "启动应用（开发模式）..."
        npm start
        ;;
    2)
        echo "打包Android应用..."
        echo "注意: 此操作需要Expo账号"
        read -p "是否已登录Expo账号? (Y/N): " login
        if [[ $login == "N" || $login == "n" ]]; then
            npx eas login
        fi
        
        read -p "是否已配置EAS构建? (Y/N): " configured
        if [[ $configured == "N" || $configured == "n" ]]; then
            echo "配置EAS构建..."
            npx eas build:configure
        fi
        
        npm run build:android
        echo
        echo "构建完成后，你可以在Expo网站上下载APK文件"
        echo "或者使用以下命令查看构建状态:"
        echo "npx eas build:list"
        ;;
    3)
        echo "打包iOS应用..."
        echo "注意: 此操作需要Expo账号和Apple Developer账号"
        read -p "是否已登录Expo账号? (Y/N): " login
        if [[ $login == "N" || $login == "n" ]]; then
            npx eas login
        fi
        
        read -p "是否已配置EAS构建? (Y/N): " configured
        if [[ $configured == "N" || $configured == "n" ]]; then
            echo "配置EAS构建..."
            npx eas build:configure
        fi
        
        npm run build:ios
        echo
        echo "构建完成后，你可以在Expo网站上下载IPA文件"
        echo "或者使用以下命令查看构建状态:"
        echo "npx eas build:list"
        ;;
    4)
        echo "打包Web应用..."
        npm run build:web
        echo "Web应用已打包到 web-build 目录"
        echo "你可以将该目录部署到任何Web服务器上"
        ;;
    5)
        echo "退出程序"
        exit 0
        ;;
    *)
        echo "无效选项，请重新运行脚本"
        ;;
esac